/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-09-10
*/
package com.rzico.order.service;

import com.rzico.account.entity.Refunds;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Product;
import com.rzico.basics.mapper.ProductMapper;
import com.rzico.exception.CustomException;
import com.rzico.order.entity.*;
import com.rzico.order.enumx.OrderStatusEnum;
import com.rzico.order.mapper.GroupPeriodMapper;
import com.rzico.order.mapper.GroupProductMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.rzico.order.mapper.GroupBuyMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * <pre>
 * 团购管理业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class GroupBuyService extends BaseServiceImpl<GroupBuy, String> {

    @Autowired
    private GroupBuyMapper groupBuyMapper;

    @Autowired
    private GroupProductMapper groupProductMapper;

    @Autowired
    private GroupPeriodMapper groupPeriodMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired @Lazy
    private GroupOrderService groupOrderService;

    @Autowired @Lazy
    private OrderService orderService;

    @Override
    public BaseMapper<GroupBuy, String> getMapper() {
        return groupBuyMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer save(GroupBuy groupBuy) {

        Integer w = groupBuyMapper.insertUseGeneratedKeys(groupBuy);
        for (GroupProduct groupProduct:groupBuy.getItemList()) {
            groupProduct.setCreateDate(new Date());
            groupProduct.setGroupBuyId(groupBuy.getId());
            Product product = productMapper.selectByPrimaryKey(groupProduct.getProductId());
            groupProduct.setName(product.getName());
            groupProduct.setSpec(product.getSpec());
            groupProduct.setUnit(product.getUnit());
            groupProduct.setThumbnail(product.getThumbnail());
            groupProductMapper.insertUseGeneratedKeys(groupProduct);
        }

        return w;

    }

    @Transactional(rollbackFor = Exception.class)
    public Integer update(GroupBuy groupBuy) {

        Integer w = groupBuyMapper.updateByPrimaryKeySelective(groupBuy);

        GroupProduct deleteProduct = new GroupProduct();
        deleteProduct.setGroupBuyId(groupBuy.getId());
        groupProductMapper.delete(deleteProduct);

        for (GroupProduct groupProduct:groupBuy.getItemList()) {
            groupProduct.setCreateDate(new Date());
            groupProduct.setGroupBuyId(groupBuy.getId());
            Product product = productMapper.selectByPrimaryKey(groupProduct.getProductId());
            groupProduct.setName(product.getName());
            groupProduct.setSpec(product.getSpec());
            groupProduct.setUnit(product.getUnit());
            groupProduct.setThumbnail(product.getThumbnail());
            groupProductMapper.insertUseGeneratedKeys(groupProduct);
        }

        return w;
    }


    @Transactional(rollbackFor = Exception.class)
    public Integer audit(GroupBuy groupBuy) {

//        if (groupBuy.getRobotNum()>groupBuy.getOrderNum()) {
//            throw new CustomException("机器人太多");
//        }

        GroupPeriod groupPeriod = new GroupPeriod();
        groupPeriod.setBuyNum(0);
        groupPeriod.setGroupBuyId(groupBuy.getId());
        groupPeriod.setGroupNum(groupBuy.getGroupNum());
        groupPeriod.setCreateDate(new Date());
        groupPeriod.setStatus(0);
        groupPeriod.setPeriodNum(1);
        groupPeriodMapper.insertUseGeneratedKeys(groupPeriod);

        groupBuy.setStatus(1);
        groupBuy.setPeriodNum(1);
        groupBuy.setPeriodId(groupPeriod.getId());
        groupBuyMapper.updateByPrimaryKeySelective(groupBuy);


        return 1;
    }


    @Transactional(rollbackFor = Exception.class)
    public Integer unAudit(GroupBuy groupBuy) {

        GroupPeriod groupPeriod = new GroupPeriod();
        groupPeriod.setGroupBuyId(groupBuy.getId());
        groupPeriodMapper.delete(groupPeriod);

        groupBuy.setStatus(0);
        groupBuy.setPeriodNum(0);
        groupBuy.setPeriodId(null);
        groupBuyMapper.updateByPrimaryKeySelective(groupBuy);

        return 1;

    }


    @Transactional(rollbackFor = Exception.class)
    public List<Refunds> cancel(GroupBuy groupBuy) {

        groupBuy.setStatus(3);
        groupBuyMapper.updateByPrimaryKeySelective(groupBuy);
        List<Refunds> refundsList = new ArrayList<>();

        GroupPeriod groupPeriod = groupPeriodMapper.selectByPrimaryKey(groupBuy.getPeriodId());
        String sn = "GP"+String.valueOf(groupPeriod.getId());

        Map<String,Object> params = new HashMap<>();
        params.put("periodId",0L);
        params.put("robot",0);
        params.put("groupBuyId",groupBuy.getId());
        List<GroupOrder> groupOrderList = groupOrderService.selectList(params);
        if (groupPeriod.getStatus().equals(0)) {
            //没有完成的订单，做退款处理，不返利
            List<GroupOrder> periodOrderList = groupOrderService.getOrderList(groupBuy.getPeriodId());
            for (GroupOrder groupOrder:periodOrderList) {
                if (groupOrder.getRobot()==false) {
                    groupOrderList.add(groupOrder);
                }
            }
        }
        //开始处理订单
        Integer w = 0;
        for (GroupOrder groupOrder:groupOrderList) {
            groupOrder.setOrderStatus(2);
            groupOrderService.updateByPrimaryKeySelective(groupOrder);
            w++;
            List<Refunds> refunds = orderService.cancel(groupOrder.getOrderId(), sn + "-" + String.valueOf(w));
            refundsList.addAll(0,refunds);
        }

        return refundsList;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {
        int rw = 0;
        for (Object id:ids) {
            GroupBuy groupBuy = selectByPrimaryKey(id);
            if (!groupBuy.getStatus().equals(0)) {
                throw new CustomException("已审核不能删除");
            }

            GroupPeriod groupPeriod = new GroupPeriod();
            groupPeriod.setGroupBuyId(groupBuy.getId());
            groupPeriodMapper.delete(groupPeriod);
            rw = rw + super.delete(groupBuy);
        }
        return rw;
    }


}
